/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.Timer;

import sudoku.controller.Controller;
import sudoku.data.SudokuButton;
import sudoku.images.ImageLoader;
import sudoku.model.ChoiceModel;
import sudoku.model.SudokuModel;
import sudoku.util.Config;
import sudoku.util.Data;
import sudoku.util.Language;
import sudoku.util.game.Solver;

/**
 * @author Romet Piho
 * 
 */
public class SudokuUI extends JFrame implements ActionListener, KeyListener, MouseListener, MouseMotionListener, WindowListener {

  /**
   * serialVersionUID
   */
  private static final long serialVersionUID = 1L;
  private JMenu kasutaja;
  private JMenu keel;
  private JMenu m2ng;
  private JMenu m2ngud;
  private JMenu seaded;
  private JMenu ajal;
  private JMenu abim;
  private JMenu genereeri;
  private JMenu view;

  private JTextField status;
  private JTextField level;

  private JMenuItem eesti;
  private JMenuItem inglise;
  private JMenuItem ajalugu;
  private JMenuItem ava_menu;
  private JMenuItem salvesta_menu;
  private JMenuItem uus_menu;
  private JMenuItem print_menu;
  private JMenuItem v2lju_menu;
  private JMenuItem abi_menu;
  private JMenuItem about;
  private JMenuItem updater;
  private JMenuItem lahenda_menu;
  private JMenuItem vali_menu9;
  private JMenuItem vali_menu16;
  private JMenuItem lahenda_yks_menu;
  private JMenuItem kasutaja_menu;
  private JMenuItem alusta_menu;
  private JMenuItem paus_menu;
  private JMenuItem undo_menu;
  private JMenuItem redo_menu;
  private JMenuItem lisa_menu;
  private JMenuItem cheat;
  private JMenuItem symm;
  private JMenuItem diffeasy;
  private JMenuItem diffmedium;
  private JMenuItem diffhard;
  private JMenuItem diffexpert;
  private JMenuItem diffextreme;
  private JMenuItem computerhelp;
  private JMenuItem fonts_menu;

  private JRadioButtonMenuItem ruudustik9;
  private JRadioButtonMenuItem ruudustik16;

  private JButton background;
  private JButton undo;
  private JButton redo;
  private JButton alusta_button;
  private JButton paus_button;
  private JButton uuesti_button;
  private JButton help1;
  private JButton help2;
  private JButton help3;
  private JButton help4;
  private JButton help5;
  private JButton help6;
  private JButton help7;
  private JButton help8;
  private JButton help9;
  private JButton help10;
  private JButton help11;
  private JButton help12;
  private JButton help13;
  private JButton help14;
  private JButton help15;
  private JButton help16;
  private JButton helpall;
  private JButton green;
  private JButton cyan;
  private JButton orange;
  private JButton pink;

  private JLabel time_label;
  private JLabel paus_label;

  public Timer m2nguTimer;
  private int sec;
  private int min;
  private int hour;

  private JPanel paus_panel;
  private JPanel panel;
  private JButton[][] ruut;
  private final int kasti_paksus = 1;
  private final Color kasti_raamid = Color.BLACK;

  private volatile Config config;
  private volatile Controller controller;
  private volatile SudokuModel model;
  private volatile ChoiceModel choiceModel;
  private volatile Language language;
  private volatile Data data;
  private HelpUI helpUI;

  private boolean abi = false;

  private Color eelmine;
  private int si;
  private int sj;

  private JScrollPane scrollPane;

  public static final Color DEFAULT = new Color(238, 238, 238);

  /**
   * Seadistab põhiakna graafika kasutajaliidese. Teeb mängu menüüd ja lisab
   * need raamile. Lisab raamile ka kogu ülejäänud graafika.
   * 
   * @param config
   *          konfiguratsiooni klassi isend
   * @param model
   *          põhiakna loogika klassi isend
   * @param controller
   *          kontrolleri klassi isend
   * @see #uusPaneel()
   */
  public SudokuUI(Language language, Config config, SudokuModel model, Controller controller, ChoiceModel choiceModel) {
    this.language = language;
    this.config = config;
    this.model = model;
    this.controller = controller;
    this.choiceModel = choiceModel;

    model.setOnAlustatud(false);
    model.setOnPaus(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    if (this.config.getLanguage().equals("est")) {
      model.setNimi("Nimetu");
    } else if (this.config.getLanguage().equals("eng")) {
      model.setNimi("Unnamed");
    }

    eesti = new JMenuItem(language.getSudoku_eesti(), new ImageIcon(ImageLoader.getImage("Estonia.png")));
    eesti.addActionListener(this);

    inglise = new JMenuItem(language.getSudoku_inglise(), new ImageIcon(ImageLoader.getImage("United-Kingdom.png")));
    inglise.addActionListener(this);

    ajalugu = new JMenuItem(language.getSudoku_ajalugu());
    ajalugu.addActionListener(this);

    ava_menu = new JMenuItem(language.getSudoku_ava_menu(), new ImageIcon(ImageLoader.getImage("Open.png")));
    ava_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    ava_menu.addActionListener(this);

    salvesta_menu = new JMenuItem(language.getSudoku_salvesta_menu(), new ImageIcon(ImageLoader.getImage("Save.png")));
    salvesta_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    salvesta_menu.addActionListener(this);

    alusta_menu = new JMenuItem(language.getSudoku_alusta_button());
    alusta_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0));
    alusta_menu.addActionListener(this);
    paus_menu = new JMenuItem(language.getSudoku_paus_button());
    paus_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0));
    paus_menu.addActionListener(this);

    uus_menu = new JMenuItem(language.getSudoku_uus_menu(), new ImageIcon(ImageLoader.getImage("New.png")));
    uus_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
    uus_menu.addActionListener(this);

    print_menu = new JMenuItem(language.getSudoku_print_menu(), new ImageIcon(ImageLoader.getImage("Print.png")));
    print_menu.addActionListener(this);

    v2lju_menu = new JMenuItem(language.getSudoku_v2lju_menu(), new ImageIcon(ImageLoader.getImage("Close.png")));
    v2lju_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    v2lju_menu.addActionListener(this);

    abi_menu = new JMenuItem(language.getSudoku_abi_menu());
    abi_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
    abi_menu.addActionListener(this);

    updater = new JMenuItem(language.getSudoku_updater());
    updater.addActionListener(this);

    lahenda_menu = new JMenuItem(language.getSudoku_lahenda_menu());
    lahenda_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0));
    lahenda_menu.addActionListener(this);

    vali_menu9 = new JMenuItem(language.getSudoku_vali_menu9());
    vali_menu9.addActionListener(this);

    vali_menu16 = new JMenuItem(language.getSudoku_vali_menu16());
    vali_menu16.addActionListener(this);

    kasutaja_menu = new JMenuItem(language.getSudoku_kasutaja_menu());
    kasutaja_menu.addActionListener(this);

    computerhelp = new JMenuItem(language.getSudoku_computerhelp());
    computerhelp.addActionListener(this);

    about = new JMenuItem(language.getSudoku_about());
    about.addActionListener(this);

    ruudustik9 = new JRadioButtonMenuItem(language.getSudoku_ruudustik9(), false);
    ruudustik9.addActionListener(this);

    ruudustik16 = new JRadioButtonMenuItem(language.getSudoku_ruudustik16(), false);
    ruudustik16.addActionListener(this);

    ButtonGroup group2 = new ButtonGroup();
    group2.add(ruudustik9);
    group2.add(ruudustik16);
    if (this.config.getRuudustik() == 9) {
      ruudustik9.setSelected(true);
    }
    if (this.config.getRuudustik() == 16) {
      ruudustik16.setSelected(true);
    }

    lahenda_yks_menu = new JMenuItem(language.getSudoku_lahenda_yks_menu());
    lahenda_yks_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
    lahenda_yks_menu.addActionListener(this);

    lisa_menu = new JMenuItem(language.getSudoku_lisa_menu());
    lisa_menu.addActionListener(this);

    cheat = new JMenuItem();
    cheat.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F6, ActionEvent.CTRL_MASK));
    cheat.setVisible(false);
    cheat.addActionListener(this);

    symm = new JMenuItem();
    symm.addActionListener(this);

    diffeasy = new JMenuItem(language.getSudoku_diffeasy());
    diffeasy.addActionListener(this);

    diffmedium = new JMenuItem(language.getSudoku_diffmedium());
    diffmedium.addActionListener(this);

    diffhard = new JMenuItem(language.getSudoku_diffhard());
    diffhard.addActionListener(this);

    diffexpert = new JMenuItem(language.getSudoku_diffexpert());
    diffexpert.addActionListener(this);

    diffextreme = new JMenuItem(language.getSudoku_diffextreme());
    diffextreme.addActionListener(this);

    symm = new JMenuItem(language.getSudoku_symm());
    symm.addActionListener(this);

    keel = new JMenu(language.getSudoku_keel());
    keel.add(eesti);
    keel.add(inglise);

    seaded = new JMenu(language.getSudoku_seaded());
    seaded.add(keel);
    seaded.addSeparator();
    seaded.add(computerhelp);
    seaded.addSeparator();
    seaded.add(symm);
    seaded.addSeparator();
    seaded.add(ruudustik9);
    seaded.add(ruudustik16);

    genereeri = new JMenu(language.getSudoku_genereeri());
    genereeri.add(diffeasy);
    genereeri.add(diffmedium);
    genereeri.add(diffhard);
    genereeri.add(diffexpert);
    genereeri.add(diffextreme);

    m2ngud = new JMenu(language.getSudoku_m2ngud());
    m2ngud.add(vali_menu9);
    m2ngud.add(vali_menu16);
    m2ngud.addSeparator();
    m2ngud.add(lisa_menu);
    m2ngud.addSeparator();
    m2ngud.add(genereeri);

    m2ng = new JMenu(language.getSudoku_m2ng());
    m2ng.setMnemonic(KeyEvent.VK_ALT);
    m2ng.add(uus_menu);
    m2ng.add(ava_menu);
    m2ng.add(salvesta_menu);
    m2ng.addSeparator();
    m2ng.add(print_menu);
    m2ng.addSeparator();
    m2ng.add(v2lju_menu);

    ajal = new JMenu(language.getSudoku_ajal());
    ajal.add(ajalugu);

    kasutaja = new JMenu(config.getNimi());
    kasutaja.add(kasutaja_menu);

    abim = new JMenu(language.getSudoku_abi());
    abim.add(lahenda_menu);
    abim.add(lahenda_yks_menu);
    abim.addSeparator();
    abim.add(abi_menu);
    abim.addSeparator();
    abim.add(updater);
    abim.add(about);

    JMenuBar mbar = new JMenuBar();

    undo_menu = new JMenuItem(language.getSudoku_undo(), new ImageIcon(ImageLoader.getImage("Undo.png")));
    undo_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
    undo_menu.addActionListener(this);
    redo_menu = new JMenuItem(language.getSudoku_redo(), new ImageIcon(ImageLoader.getImage("Redo.png")));
    redo_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
    redo_menu.addActionListener(this);

    background = new JButton("      ");
    background.setFocusPainted(false);
    background.addActionListener(this);
    background.setToolTipText(language.getsudoku_background());

    undo = new JButton(new ImageIcon(ImageLoader.getImage("Undo.png")));
    undo.setFocusPainted(false);
    undo.addActionListener(this);

    redo = new JButton(new ImageIcon(ImageLoader.getImage("Redo.png")));
    redo.setFocusPainted(false);
    redo.addActionListener(this);

    undo.setMinimumSize(new Dimension(33, 28));
    redo.setMinimumSize(new Dimension(33, 28));

    help1 = new JButton("  1  ");
    help1.setFocusPainted(false);
    help1.addActionListener(this);
    help1.addMouseListener(this);

    help2 = new JButton("  2  ");
    help2.setFocusPainted(false);
    help2.addActionListener(this);
    help2.addMouseListener(this);

    help3 = new JButton("  3  ");
    help3.setFocusPainted(false);
    help3.addActionListener(this);
    help3.addMouseListener(this);

    help4 = new JButton("  4  ");
    help4.setFocusPainted(false);
    help4.addActionListener(this);
    help4.addMouseListener(this);

    help5 = new JButton("  5  ");
    help5.setFocusPainted(false);
    help5.addActionListener(this);
    help5.addMouseListener(this);

    help6 = new JButton("  6  ");
    help6.setFocusPainted(false);
    help6.addActionListener(this);
    help6.addMouseListener(this);

    help7 = new JButton("  7  ");
    help7.setFocusPainted(false);
    help7.addActionListener(this);
    help7.addMouseListener(this);

    help8 = new JButton("  8  ");
    help8.setFocusPainted(false);
    help8.addActionListener(this);
    help8.addMouseListener(this);

    help9 = new JButton("  9  ");
    help9.setFocusPainted(false);
    help9.addActionListener(this);
    help9.addMouseListener(this);

    help10 = new JButton(" 10  ");
    help10.setFocusPainted(false);
    help10.addActionListener(this);
    help10.addMouseListener(this);

    help11 = new JButton(" 11  ");
    help11.setFocusPainted(false);
    help11.addActionListener(this);
    help11.addMouseListener(this);

    help12 = new JButton(" 12  ");
    help12.setFocusPainted(false);
    help12.addActionListener(this);
    help12.addMouseListener(this);

    help13 = new JButton(" 13  ");
    help13.setFocusPainted(false);
    help13.addActionListener(this);
    help13.addMouseListener(this);

    help14 = new JButton(" 14  ");
    help14.setFocusPainted(false);
    help14.addActionListener(this);
    help14.addMouseListener(this);

    help15 = new JButton(" 15  ");
    help15.setFocusPainted(false);
    help15.addActionListener(this);
    help15.addMouseListener(this);

    help16 = new JButton(" 16  ");
    help16.setFocusPainted(false);
    help16.addActionListener(this);
    help16.addMouseListener(this);

    helpall = new JButton("1...");
    helpall.setFocusPainted(false);
    helpall.addActionListener(this);
    helpall.addMouseListener(this);

    green = new JButton("      ");
    green.setFocusPainted(false);
    green.setBackground(Config.GREEN);
    green.addActionListener(this);

    cyan = new JButton("      ");
    cyan.setFocusPainted(false);
    cyan.setBackground(Config.CYAN);
    cyan.addActionListener(this);

    orange = new JButton("      ");
    orange.setFocusPainted(false);
    orange.setBackground(Config.ORANGE);
    orange.addActionListener(this);

    pink = new JButton("      ");
    pink.setFocusPainted(false);
    pink.setBackground(Config.PINK);
    pink.addActionListener(this);

    fonts_menu = new JMenuItem(this.language.getSudoku_fonts_menu());
    fonts_menu.addActionListener(this);

    view = new JMenu(this.language.getSudoku_view());
    view.add(undo_menu);
    view.add(redo_menu);
    view.addSeparator();
    view.add(alusta_menu);
    view.add(paus_menu);
    view.addSeparator();
    view.add(fonts_menu);

    mbar.add(m2ng);
    mbar.add(view);
    mbar.add(seaded);
    mbar.add(m2ngud);
    mbar.add(ajal);
    mbar.add(kasutaja);
    mbar.add(abim);
    mbar.add(undo);
    mbar.add(redo);
    mbar.add(cheat);

    JToolBar tbar = new JToolBar();
    tbar.setFloatable(false);
    tbar.add(undo);
    tbar.add(redo);
    tbar.add(background);
    tbar.add(help1);
    tbar.add(help1);
    tbar.add(help2);
    tbar.add(help3);
    tbar.add(help4);
    tbar.add(help5);
    tbar.add(help6);
    tbar.add(help7);
    tbar.add(help8);
    tbar.add(help9);
    tbar.add(help10);
    tbar.add(help11);
    tbar.add(help12);
    tbar.add(help13);
    tbar.add(help14);
    tbar.add(help15);
    tbar.add(help16);
    tbar.add(helpall);
    tbar.add(green);
    tbar.add(cyan);
    tbar.add(orange);
    tbar.add(pink);

    level = new JTextField(6);
    level.setEditable(false);
    level.setFocusable(false);
    level.setFont(level.getFont().deriveFont(25f));
    level.setFont(level.getFont().deriveFont(Font.BOLD));
    panel = uusPaneel();

    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.1;
    c.weighty = 0.1;
    p.add(mbar, c);
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 111.1;
    c.weighty = 111.1;
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    p.add(tbar, c);

    status = new JTextField("");
    status.setEditable(false);
    status.addMouseMotionListener(this);
    status.addMouseListener(this);
    status.setSelectionColor(Color.CYAN);
    status.setBackground(DEFAULT);

    scrollPane = new JScrollPane(status);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    JPanel pp = new JPanel();
    pp.setLayout(new GridBagLayout());
    GridBagConstraints cc = new GridBagConstraints();
    cc.gridx = 0;
    cc.gridy = 0;
    cc.gridwidth = 2;
    cc.gridheight = 1;
    cc.weightx = 1;
    cc.weighty = 1;
    cc.anchor = GridBagConstraints.LINE_START;
    cc.fill = GridBagConstraints.BOTH;
    pp.add(scrollPane, cc);
    cc.gridx = 2;
    cc.gridy = 0;
    cc.gridwidth = 1;
    cc.gridheight = 1;
    cc.weightx = 0;
    cc.weighty = 1;
    cc.anchor = GridBagConstraints.LINE_END;
    cc.fill = GridBagConstraints.BOTH;
    pp.add(level, cc);

    add(p, BorderLayout.NORTH);
    add(panel, BorderLayout.CENTER);
    add(pp, BorderLayout.SOUTH);
    seadistaLahendajad();
    seadistaUndoRedo();
    addWindowListener(this);

    setIconImage(ImageLoader.getImage("icon.gif"));
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(m2nguTimer)) {
      timer();
    } else if (e.getSource().equals(help1)) {
      helpbuttons(1);
    } else if (e.getSource().equals(help2)) {
      helpbuttons(2);
    } else if (e.getSource().equals(help3)) {
      helpbuttons(3);
    } else if (e.getSource().equals(help4)) {
      helpbuttons(4);
    } else if (e.getSource().equals(help5)) {
      helpbuttons(5);
    } else if (e.getSource().equals(help6)) {
      helpbuttons(6);
    } else if (e.getSource().equals(help7)) {
      helpbuttons(7);
    } else if (e.getSource().equals(help8)) {
      helpbuttons(8);
    } else if (e.getSource().equals(help9)) {
      helpbuttons(9);
    } else if (e.getSource().equals(help10)) {
      helpbuttons(10);
    } else if (e.getSource().equals(help11)) {
      helpbuttons(11);
    } else if (e.getSource().equals(help12)) {
      helpbuttons(12);
    } else if (e.getSource().equals(help13)) {
      helpbuttons(13);
    } else if (e.getSource().equals(help14)) {
      helpbuttons(14);
    } else if (e.getSource().equals(help15)) {
      helpbuttons(15);
    } else if (e.getSource().equals(help16)) {
      helpbuttons(16);
    } else if (e.getSource().equals(helpall)) {
      helpbuttons(999);
    } else if (e.getSource().equals(green)) {
      if (green.getBackground().equals(Color.WHITE)) {
        green.setBackground(Config.GREEN);
        status("");
      } else {
        green.setBackground(Color.WHITE);
        status(language.getSudoku_statusgreen(), Config.GREEN);
      }
      cyan.setBackground(Config.CYAN);
      orange.setBackground(Config.ORANGE);
      pink.setBackground(Config.PINK);
    } else if (e.getSource().equals(cyan)) {
      if (cyan.getBackground().equals(Color.WHITE)) {
        cyan.setBackground(Config.CYAN);
        status("");
      } else {
        cyan.setBackground(Color.WHITE);
        status(language.getSudoku_statuscyan(), Config.CYAN);
      }
      green.setBackground(Config.GREEN);
      orange.setBackground(Config.ORANGE);
      pink.setBackground(Config.PINK);
    } else if (e.getSource().equals(orange)) {
      if (orange.getBackground().equals(Color.WHITE)) {
        orange.setBackground(Config.ORANGE);
        status("");
      } else {
        orange.setBackground(Color.WHITE);
        status(language.getSudoku_statusorange(), Config.ORANGE);
      }
      cyan.setBackground(Config.CYAN);
      green.setBackground(Config.GREEN);
      pink.setBackground(Config.PINK);
    } else if (e.getSource().equals(pink)) {
      if (pink.getBackground().equals(Color.WHITE)) {
        pink.setBackground(Config.PINK);
        status("");
      } else {
        pink.setBackground(Color.WHITE);
        status(language.getSudoku_statuspink(), Config.PINK);
      }
      cyan.setBackground(Config.CYAN);
      green.setBackground(Config.GREEN);
      orange.setBackground(Config.ORANGE);
    } else if (e.getSource().equals(symm)) {
      controller.handleEvent(Controller.SYMMETRY_EVENT);
    }
    // else if (e.getSource().equals(cheat)){
    // model.lahendaCheat();
    // }
    else if (e.getSource().equals(lisa_menu)) {
      controller.handleEvent(Controller.LISA_EVENT);
    } else if (e.getSource().equals(background)) {
      if (controller.getChoiceUI() != null && controller.getChoiceUI().getDialog().isVisible()) {
        controller.getChoiceUI().deleteKey();
      } else {
        uuendaBackgroundAll();
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        green.setBackground(Config.GREEN);
        cyan.setBackground(Config.CYAN);
        orange.setBackground(Config.ORANGE);
        pink.setBackground(Config.PINK);
        status("");
      }
    } else if (undo.isEnabled() && (e.getSource().equals(undo) || e.getSource().equals(undo_menu))) {
      controller.handleEvent(Controller.UNDO_EVENT);
    } else if (redo.isEnabled() && (e.getSource().equals(redo) || e.getSource().equals(redo_menu))) {
      controller.handleEvent(Controller.REDO_EVENT);
    } else if (e.getSource().equals(eesti)) {
      controller.handleEvent(Controller.KEEL_EESTI_EVENT);
    } else if (e.getSource().equals(inglise)) {
      controller.handleEvent(Controller.KEEL_INGLISE_EVENT);
    } else if (e.getSource().equals(diffeasy)) {
      model.setLastLevel(model.getLevel());
      model.setLevel(language.getSudoku_diffeasy());
      controller.handleEvent(Controller.GENEREERI_M2NG_EVENT);
    } else if (e.getSource().equals(diffmedium)) {
      model.setLastLevel(model.getLevel());
      model.setLevel(language.getSudoku_diffmedium());
      controller.handleEvent(Controller.GENEREERI_M2NG_EVENT);
    } else if (e.getSource().equals(diffhard)) {
      model.setLastLevel(model.getLevel());
      model.setLevel(language.getSudoku_diffhard());
      controller.handleEvent(Controller.GENEREERI_M2NG_EVENT);
    } else if (e.getSource().equals(diffexpert)) {
      model.setLastLevel(model.getLevel());
      model.setLevel(language.getSudoku_diffexpert());
      controller.handleEvent(Controller.GENEREERI_M2NG_EVENT);
    } else if (e.getSource().equals(diffextreme)) {
      model.setLastLevel(model.getLevel());
      model.setLevel(language.getSudoku_diffextreme());
      controller.handleEvent(Controller.GENEREERI_M2NG_EVENT);
    } else if (e.getSource().equals(ruudustik9)) {
      controller.handleEvent(Controller.RUUDUSTIK9_EVENT);
    } else if (e.getSource().equals(ruudustik16)) {
      controller.handleEvent(Controller.RUUDUSTIK16_EVENT);
    } else if (e.getSource().equals(print_menu)) {
      controller.handleEvent(Controller.PRINT_PREVIEW_EVENT);
    } else if (e.getSource().equals(uus_menu)) {
      controller.handleEvent(Controller.UUS_M2NG_EVENT);
    } else if (e.getSource().equals(alusta_button) || e.getSource().equals(alusta_menu)) {
      controller.handleEvent(Controller.ALUSTA_M2NG_EVENT);
    } else if (e.getSource().equals(abi_menu)) {
      controller.handleEvent(Controller.ABI_MENU_EVENT);
    } else if (e.getSource().equals(about)) {
      controller.handleEvent(Controller.ABOUT_MENU_EVENT);
    } else if (e.getSource().equals(updater)) {
      controller.handleEvent(Controller.UUENDA_EVENT);
    } else if (e.getSource().equals(vali_menu9)) {
      data.setGames(9);
      controller.handleEvent(Controller.VALI_M2NG_EVENT);
    } else if (e.getSource().equals(vali_menu16)) {
      data.setGames(16);
      controller.handleEvent(Controller.VALI_M2NG_EVENT);
    } else if (e.getSource().equals(computerhelp)) {
      controller.handleEvent(Controller.COMPUTERHELP_EVENT);
    } else if (e.getSource().equals(ajalugu)) {
      controller.handleEvent(Controller.AJALUGU_EVENT);
    } else if (e.getSource().equals(uuesti_button)) {
      controller.handleEvent(Controller.UUESTI_EVENT);
    } else if (e.getSource().equals(paus_button) || e.getSource().equals(paus_menu)) {
      controller.handleEvent(Controller.PAUS_EVENT);
    } else if (e.getSource().equals(v2lju_menu)) {
      if (model.thread != null && model.thread.isAlive()) {
        model.generate.done = true;
      }
      dispose();
    } else if (e.getSource().equals(salvesta_menu)) {
      controller.handleEvent(Controller.SALVESTA_M2NG_EVENT);
    } else if (e.getSource().equals(ava_menu)) {
      controller.handleEvent(Controller.AVA_M2NG_EVENT);
    } else if (e.getSource().equals(lahenda_menu)) {
      removeLightgray();
      controller.handleEvent(Controller.LAHENDA_M2NG_EVENT);
    } else if (e.getSource().equals(lahenda_yks_menu)) {
      removeLightgray();
      controller.handleEvent(Controller.LAHENDA_SAMM_EVENT);
    } else if (e.getSource().equals(kasutaja_menu)) {
      controller.handleEvent(Controller.VALI_KASUTAJA_EVENT);
    } else if (e.getSource().equals(fonts_menu)) {
      controller.handleEvent(Controller.FONTSIZE_EVENT);
    }
  }

  /**
   * Antud nupuga seotud nuppudele paneb vajalikesse kohtadesse antud numbri
   * abinupuna
   * 
   * @param i
   * @param j
   * @param button
   *          etteantud number
   */
  private void addDep(int i, int j, String button) {
    new Solver(config, model, config.getRuudustik(), config.getKast(), true);
    for (int k = 0; k < ruut.length; k++) {
      if (model.getSudokuButtons()[i][k].getNumber().equals(" ")
          && model.getSudokuButtons()[i][k].getKandidaat(Integer.parseInt(button) - 1)) {
        ((JLabel) ruut[i][k].getComponent(Integer.parseInt(button) - 1)).setText(button);
      }
      if (model.getSudokuButtons()[k][j].getNumber().equals(" ")
          && model.getSudokuButtons()[k][j].getKandidaat(Integer.parseInt(button) - 1)) {
        ((JLabel) ruut[k][j].getComponent(Integer.parseInt(button) - 1)).setText(button);
      }
    }
    int rida = (i / config.getKast()) * config.getKast();
    int veerg = (j / config.getKast()) * config.getKast();
    for (int k = rida; k < rida + config.getKast(); k++) {
      for (int l = veerg; l < veerg + config.getKast(); l++) {
        if (model.getSudokuButtons()[k][l].getNumber().equals(" ")
            && model.getSudokuButtons()[k][l].getKandidaat(Integer.parseInt(button) - 1)) {
          ((JLabel) ruut[k][l].getComponent(Integer.parseInt(button) - 1)).setText(button);
        }
      }
    }
  }

  /**
   * Lisab kõik antud numbri abinupud, kuhu on vaja
   * 
   * @param button
   *          antud number
   */
  private void addHelpbutton(String button) {
    removeHelpbutton(button);
    new Solver(config, model, config.getRuudustik(), config.getKast(), true);
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (model.getSudokuButtons()[i][j].getNumber().equals(" ")) {
          if (model.getSudokuButtons()[i][j].getKandidaat(Integer.parseInt(button) - 1)) {
            final JLabel l = (JLabel) ruut[i][j].getComponent(Integer.parseInt(button) - 1);
            l.setText(button);
            l.setOpaque(false);
            l.setBackground(Color.WHITE);
          }
        }
      }
    }
  }

  /**
   * Taastada vastava nupu kõik vajalikud abinupud
   * 
   * @param i
   * @param j
   */
  private void addHelpbutton(int i, int j) {
    for (int k = 0; k < config.getRuudustik(); k++) {
      ((JLabel) ruut[i][j].getComponent(k)).setText(" ");
    }
    for (int k = 0; k < config.getRuudustik(); k++) {
      if (model.getSudokuButtons()[i][j].getKandidaat(k) && addHelpbuttonNeedsWriting(k + 1)) {
        ((JLabel) ruut[i][j].getComponent(k)).setText(String.valueOf(k + 1));
      }
    }
  }

  /**
   * Kas vastavat abinuppu on vaja uuesti trükkida
   * 
   * @param i
   * @return
   */
  private boolean addHelpbuttonNeedsWriting(int i) {
    if (i == 1 && help1.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 2 && help2.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 3 && help3.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 4 && help4.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 5 && help5.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 6 && help6.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 7 && help7.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 8 && help8.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 9 && help9.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 10 && help10.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 11 && help11.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 12 && help12.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 13 && help13.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 14 && help14.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 15 && help15.getBackground().equals(Color.WHITE)) {
      return true;
    } else if (i == 16 && help16.getBackground().equals(Color.WHITE)) {
      return true;
    }
    return false;
  }

  /**
   * Muudab kõik antud numbriga helpbuttonite backgroundi lightgreeniks. Jätab
   * välja hinteri ja värvijate nupud
   * 
   * @param button
   */
  private void colorHelpbutton(String button) {
    uuendaBackgroundLightgreen();
    new Solver(config, model, config.getRuudustik(), config.getKast(), true);
    if (button.equals("999")) {
      button = getMinHelpButtons("*");
    }

    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (model.getSudokuButtons()[i][j].getNumber().equals(" ") && containsHelpButton(ruut[i][j], button)
            && !ruut[i][j].getBackground().equals(Color.YELLOW) && !ruut[i][j].getBackground().equals(Color.GREEN)
            && !ruut[i][j].getBackground().equals(Config.CYAN) && !ruut[i][j].getBackground().equals(Config.GREEN)
            && !ruut[i][j].getBackground().equals(Config.ORANGE) && !ruut[i][j].getBackground().equals(Config.PINK)) {
          ruut[i][j].setBackground(Config.LIGHTGREEN);
        }
      }
    }
  }

  private String getMinHelpButtons(String buttons) {
    if (buttons.equals("****")) {
      return buttons;
    }
    boolean found = false;
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (model.getSudokuButtons()[i][j].getNumber().equals(" ") && containsHelpButton(ruut[i][j], buttons)) {
          found = true;
          break;
        }
      }
      if (found) {
        break;
      }
    }
    if (!found) {
      return getMinHelpButtons(buttons + "*");
    }
    return buttons;
  }

  private boolean containsHelpButton(JButton button, String text) {
    if (text.contains("*")) {
      int nr = 0;
      for (Component c : button.getComponents()) {
        String t = ((JLabel) c).getText().trim();
        if (!t.isEmpty()) {
          nr++;
        }
      }
      return nr == text.length();
    }
    return ((JLabel) button.getComponent(Integer.parseInt(text) - 1)).getText().equals(text);
  }

  /**
   * Sätib nupud ja timeri algseisu.
   */
  public void alustaM2ng() {
    uuesti_button.setEnabled(true);
    paus_button.setEnabled(true);
    paus_menu.setEnabled(true);
    alusta_button.setEnabled(false);
    alusta_menu.setEnabled(false);
    model.setOnAlustatud(true);
    m2nguTimer.start();
    setLevel();
  }

  /**
   * Tagasab vastava nupu abinuppude arvu
   * 
   * @param i
   * @param j
   * @return tagasab vastava nupu abinuppude arvu
   */
  public int getHelpButtonsCount(int i, int j) {
    int count = 0;
    for (int k = 0; k < ruut[i][j].getComponentCount(); k++) {
      if (!((JLabel) ruut[i][j].getComponent(k)).getText().substring(0, 1).equals(" ")) {
        count++;
      }
    }
    return count;
  }

  /**
   * Getter
   * 
   * @return nuppude paneel
   */
  public JPanel getPanel() {
    return panel;
  }

  /**
   * Getter
   * 
   * @return 16x16 nupp
   */
  public JRadioButtonMenuItem getRuudustik16() {
    return ruudustik16;
  }

  /**
   * Getter
   * 
   * @return 9x9 nupp
   */
  public JRadioButtonMenuItem getRuudustik9() {
    return ruudustik9;
  }

  /**
   * Tagastab nupud.
   * 
   * @return nupud
   */
  public JButton[][] getRuut() {
    return ruut;
  }

  /**
   * Getter
   * 
   * @return statuse tekst
   */
  public String getStatusTekst() {
    return status.getText();
  }

  /**
   * Getter
   * 
   * @return statuse värv
   */
  public Color getStatusColor() {
    return status.getBackground();
  }

  /**
   * Tagastab timeri näidu.
   * 
   * @return timeri näit
   */
  public JLabel getTime_label() {
    return time_label;
  }

  /**
   * Kas eemaldab mingid helpbuttonid või lisab need. Teeb ise backgroundi
   * vahetuse.
   * 
   * @param button
   */
  private void helpbuttons(int button) {
    if (button == 1) {
      if (help1.getBackground().equals(DEFAULT)) {
        addHelpbutton("1");
        help1.setBackground(Color.WHITE);
      } else {
        if (help1.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("1");
        help1.setBackground(DEFAULT);
      }
    } else if (button == 2) {
      if (help2.getBackground().equals(DEFAULT)) {
        addHelpbutton("2");
        help2.setBackground(Color.WHITE);
      } else {
        if (help2.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("2");
        help2.setBackground(DEFAULT);
      }
    } else if (button == 3) {
      if (help3.getBackground().equals(DEFAULT)) {
        addHelpbutton("3");
        help3.setBackground(Color.WHITE);
      } else {
        if (help3.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("3");
        help3.setBackground(DEFAULT);
      }
    } else if (button == 4) {
      if (help4.getBackground().equals(DEFAULT)) {
        addHelpbutton("4");
        help4.setBackground(Color.WHITE);
      } else {
        if (help4.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("4");
        help4.setBackground(DEFAULT);
      }
    } else if (button == 5) {
      if (help5.getBackground().equals(DEFAULT)) {
        addHelpbutton("5");
        help5.setBackground(Color.WHITE);
      } else {
        if (help5.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("5");
        help5.setBackground(DEFAULT);
      }
    } else if (button == 6) {
      if (help6.getBackground().equals(DEFAULT)) {
        addHelpbutton("6");
        help6.setBackground(Color.WHITE);
      } else {
        if (help6.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("6");
        help6.setBackground(DEFAULT);
      }
    } else if (button == 7) {
      if (help7.getBackground().equals(DEFAULT)) {
        addHelpbutton("7");
        help7.setBackground(Color.WHITE);
      } else {
        if (help7.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("7");
        help7.setBackground(DEFAULT);
      }
    } else if (button == 8) {
      if (help8.getBackground().equals(DEFAULT)) {
        addHelpbutton("8");
        help8.setBackground(Color.WHITE);
      } else {
        if (help8.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("8");
        help8.setBackground(DEFAULT);
      }
    } else if (button == 9) {
      if (help9.getBackground().equals(DEFAULT)) {
        addHelpbutton("9");
        help9.setBackground(Color.WHITE);
      } else {
        if (help9.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("9");
        help9.setBackground(DEFAULT);
      }
    } else if (button == 10) {
      if (help10.getBackground().equals(DEFAULT)) {
        addHelpbutton("10");
        help10.setBackground(Color.WHITE);
      } else {
        if (help10.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("10");
        help10.setBackground(DEFAULT);
      }
    } else if (button == 11) {
      if (help11.getBackground().equals(DEFAULT)) {
        addHelpbutton("11");
        help11.setBackground(Color.WHITE);
      } else {
        if (help11.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("11");
        help11.setBackground(DEFAULT);
      }
    } else if (button == 12) {
      if (help12.getBackground().equals(DEFAULT)) {
        addHelpbutton("12");
        help12.setBackground(Color.WHITE);
      } else {
        if (help12.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("12");
        help12.setBackground(DEFAULT);
      }
    } else if (button == 13) {
      if (help13.getBackground().equals(DEFAULT)) {
        addHelpbutton("13");
        help13.setBackground(Color.WHITE);
      } else {
        if (help13.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("13");
        help13.setBackground(DEFAULT);
      }
    } else if (button == 14) {
      if (help14.getBackground().equals(DEFAULT)) {
        addHelpbutton("14");
        help14.setBackground(Color.WHITE);
      } else {
        if (help14.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("14");
        help14.setBackground(DEFAULT);
      }
    } else if (button == 15) {
      if (help15.getBackground().equals(DEFAULT)) {
        addHelpbutton("15");
        help15.setBackground(Color.WHITE);
      } else {
        if (help15.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("15");
        help15.setBackground(DEFAULT);
      }
    } else if (button == 16) {
      if (help16.getBackground().equals(DEFAULT)) {
        addHelpbutton("16");
        help16.setBackground(Color.WHITE);
      } else {
        if (help16.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        removeHelpbutton("16");
        help16.setBackground(DEFAULT);
      }
    } else if (button == 999) {
      if (helpall.getBackground().equals(DEFAULT)) {
        help1.setBackground(DEFAULT);
        help2.setBackground(DEFAULT);
        help3.setBackground(DEFAULT);
        help4.setBackground(DEFAULT);
        help5.setBackground(DEFAULT);
        help6.setBackground(DEFAULT);
        help7.setBackground(DEFAULT);
        help8.setBackground(DEFAULT);
        help9.setBackground(DEFAULT);
        helpbuttons(1);
        helpbuttons(2);
        helpbuttons(3);
        helpbuttons(4);
        helpbuttons(5);
        helpbuttons(6);
        helpbuttons(7);
        helpbuttons(8);
        helpbuttons(9);
        if (config.getRuudustik() == 16) {
          help10.setBackground(DEFAULT);
          help11.setBackground(DEFAULT);
          help12.setBackground(DEFAULT);
          help13.setBackground(DEFAULT);
          help14.setBackground(DEFAULT);
          help15.setBackground(DEFAULT);
          help16.setBackground(DEFAULT);
          helpbuttons(10);
          helpbuttons(11);
          helpbuttons(12);
          helpbuttons(13);
          helpbuttons(14);
          helpbuttons(15);
          helpbuttons(16);
        }
        helpall.setBackground(Color.WHITE);
      } else {
        if (helpall.getForeground().equals(Config.GREEN)) {
          helpButtonsColors(button);
        }
        help1.setBackground(Color.WHITE);
        help2.setBackground(Color.WHITE);
        help3.setBackground(Color.WHITE);
        help4.setBackground(Color.WHITE);
        help5.setBackground(Color.WHITE);
        help6.setBackground(Color.WHITE);
        help7.setBackground(Color.WHITE);
        help8.setBackground(Color.WHITE);
        help9.setBackground(Color.WHITE);
        helpbuttons(1);
        helpbuttons(2);
        helpbuttons(3);
        helpbuttons(4);
        helpbuttons(5);
        helpbuttons(6);
        helpbuttons(7);
        helpbuttons(8);
        helpbuttons(9);
        if (config.getRuudustik() == 16) {
          help10.setBackground(Color.WHITE);
          help11.setBackground(Color.WHITE);
          help12.setBackground(Color.WHITE);
          help13.setBackground(Color.WHITE);
          help14.setBackground(Color.WHITE);
          help15.setBackground(Color.WHITE);
          help16.setBackground(Color.WHITE);
          helpbuttons(10);
          helpbuttons(11);
          helpbuttons(12);
          helpbuttons(13);
          helpbuttons(14);
          helpbuttons(15);
          helpbuttons(16);
        }
        helpall.setBackground(DEFAULT);
      }
    }
    uuendaUI();
  }

  /**
   * Kas värvib helpbuttonite backgroundid heleroheliseks või valgeks. Teeb ise
   * teksti värvi vahetuse.
   * 
   * @param i
   */
  private void helpButtonsColors(int i) {
    if (i == 1) {
      if (help1.getForeground().equals(Config.GREEN)) {
        help1.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help1.setForeground(Config.GREEN);
        colorHelpbutton("1");
      }
    } else if (i == 2) {
      if (help2.getForeground().equals(Config.GREEN)) {
        help2.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help2.setForeground(Config.GREEN);
        colorHelpbutton("2");
      }
    } else if (i == 3) {
      if (help3.getForeground().equals(Config.GREEN)) {
        help3.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help3.setForeground(Config.GREEN);
        colorHelpbutton("3");
      }
    } else if (i == 4) {
      if (help4.getForeground().equals(Config.GREEN)) {
        help4.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help4.setForeground(Config.GREEN);
        colorHelpbutton("4");
      }
    } else if (i == 5) {
      if (help5.getForeground().equals(Config.GREEN)) {
        help5.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help5.setForeground(Config.GREEN);
        colorHelpbutton("5");
      }
    } else if (i == 6) {
      if (help6.getForeground().equals(Config.GREEN)) {
        help6.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help6.setForeground(Config.GREEN);
        colorHelpbutton("6");
      }
    } else if (i == 7) {
      if (help7.getForeground().equals(Config.GREEN)) {
        help7.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help7.setForeground(Config.GREEN);
        colorHelpbutton("7");
      }
    } else if (i == 8) {
      if (help8.getForeground().equals(Config.GREEN)) {
        help8.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help8.setForeground(Config.GREEN);
        colorHelpbutton("8");
      }
    } else if (i == 9) {
      if (help9.getForeground().equals(Config.GREEN)) {
        help9.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help9.setForeground(Config.GREEN);
        colorHelpbutton("9");
      }
    } else if (i == 10) {
      if (help10.getForeground().equals(Config.GREEN)) {
        help10.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help10.setForeground(Config.GREEN);
        colorHelpbutton("10");
      }
    } else if (i == 11) {
      if (help11.getForeground().equals(Config.GREEN)) {
        help11.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help11.setForeground(Config.GREEN);
        colorHelpbutton("11");
      }
    } else if (i == 12) {
      if (help12.getForeground().equals(Config.GREEN)) {
        help12.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help12.setForeground(Config.GREEN);
        colorHelpbutton("12");
      }
    } else if (i == 13) {
      if (help13.getForeground().equals(Config.GREEN)) {
        help13.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help13.setForeground(Config.GREEN);
        colorHelpbutton("13");
      }
    } else if (i == 14) {
      if (help14.getForeground().equals(Config.GREEN)) {
        help14.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help14.setForeground(Config.GREEN);
        colorHelpbutton("14");
      }
    } else if (i == 15) {
      if (help15.getForeground().equals(Config.GREEN)) {
        help15.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help15.setForeground(Config.GREEN);
        colorHelpbutton("15");
      }
    } else if (i == 16) {
      if (help16.getForeground().equals(Config.GREEN)) {
        help16.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        help16.setForeground(Config.GREEN);
        colorHelpbutton("16");
      }
    } else if (i == 999) {
      if (helpall.getForeground().equals(Config.GREEN)) {
        helpall.setForeground(Color.BLACK);
        uuendaBackgroundLightgreen();
      } else {
        helpall.setForeground(Config.GREEN);
        colorHelpbutton("999");
      }
    }
  }

  /**
   * Uuendab nupu kirjutamisel vajalikke helpbuttoneid
   * 
   * @param uus
   */
  private void helpbuttonUpdateUus(String uus) {
    if (uus.equals(" ")) {
      addHelpbutton(model.getValik_i(), model.getValik_j());
    } else {
      removeDep(model.getValik_i(), model.getValik_j(), uus);
    }
  }

  /**
   * Uuendab nupu kirjutamisel vajalikke helpbuttoneid
   * 
   * @param vana
   */
  private void helpbuttonUpdateVana(String vana) {
    addDep(model.getValik_i(), model.getValik_j(), vana);
  }

  /**
   * Nooleklavid ja ctrl+nuber kuulajad.
   * 
   * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
   */
  @Override
  public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_1 || e.getKeyCode() == KeyEvent.VK_NUMPAD1) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(1);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_2 || e.getKeyCode() == KeyEvent.VK_NUMPAD2) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(2);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_3 || e.getKeyCode() == KeyEvent.VK_NUMPAD3) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(3);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_4 || e.getKeyCode() == KeyEvent.VK_NUMPAD4) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(4);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_5 || e.getKeyCode() == KeyEvent.VK_NUMPAD5) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(5);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_6 || e.getKeyCode() == KeyEvent.VK_NUMPAD6) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(6);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_7 || e.getKeyCode() == KeyEvent.VK_NUMPAD7) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(7);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_8 || e.getKeyCode() == KeyEvent.VK_NUMPAD8) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(8);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_9 || e.getKeyCode() == KeyEvent.VK_NUMPAD9) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
              model.setAbi(true);
              model.setValik_i(i);
              model.setValik_j(j);
              choiceModel.uusModel();
              choiceModel.setValik_i(9);
              controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            }
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            ruut[i][j].setBackground(eelmine);
            if (j == config.getRuudustik() - 1) {
              j = 0;
              if (i == config.getRuudustik() - 1) {
                i = 0;
              } else {
                i++;
              }
            } else {
              j++;
            }
            ruut[i][j].grabFocus();
            eelmine = ruut[i][j].getBackground();
            ruut[i][j].setBackground(Color.LIGHT_GRAY);
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            ruut[i][j].setBackground(eelmine);
            if (j == 0) {
              j = config.getRuudustik() - 1;
              if (i == 0) {
                i = config.getRuudustik() - 1;
              } else {
                i--;
              }
            } else {
              j--;
            }
            ruut[i][j].grabFocus();
            eelmine = ruut[i][j].getBackground();
            ruut[i][j].setBackground(Color.LIGHT_GRAY);
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            ruut[i][j].setBackground(eelmine);
            if (i == config.getRuudustik() - 1) {
              i = 0;
              if (j == 0) {
                j = config.getRuudustik() - 1;
              } else {
                j--;
              }
            } else {
              i++;
            }
            ruut[i][j].grabFocus();
            eelmine = ruut[i][j].getBackground();
            ruut[i][j].setBackground(Color.LIGHT_GRAY);
            return;
          }
        }
      }
    } else if (e.getKeyCode() == KeyEvent.VK_UP) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            ruut[i][j].setBackground(eelmine);
            if (i == 0) {
              i = config.getRuudustik() - 1;
              if (j == config.getRuudustik() - 1) {
                j = 0;
              } else {
                j++;
              }
            } else {
              i--;
            }
            ruut[i][j].grabFocus();
            eelmine = ruut[i][j].getBackground();
            ruut[i][j].setBackground(Color.LIGHT_GRAY);
            return;
          }
        }
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
   */
  @Override
  public void keyReleased(KeyEvent e) {
  }

  /**
   * Tavalised numbrikuulajad ning backspace ja delete ka.
   * 
   * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
   */
  @Override
  public void keyTyped(KeyEvent e) {
    if (e.getKeyChar() == '1') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(1);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '2') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(2);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '3') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(3);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '4') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(4);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '5') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(5);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '6') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(6);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '7') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(7);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '8') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(8);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    } else if (e.getKeyChar() == '9') {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            choiceModel.setValik_i(9);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    }
    if (e.getKeyChar() == KeyEvent.VK_DELETE) {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (ruut[i][j].equals(e.getSource())) {
            model.setValik_i(i);
            model.setValik_j(j);
            choiceModel.uusModel();
            resetAll(i, j);
            choiceModel.setValik_i(0);
            controller.handleEvent(Controller.KIRJUTA_NUMBER_EVENT);
            return;
          }
        }
      }
    }
    if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
      if (undo.isEnabled()) {
        controller.handleEvent(Controller.UNDO_EVENT);
      }
    }
  }

  /**
   * Kirjutab numbri valitud nupule.
   */
  public void kirjutaNumber() {
    if (model.isAbi() && model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].isAbiNupp()) {
      if (!model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber().equals(" ")) {
        boolean leidub = false;
        if (((JLabel) ruut[model.getValik_i()][model.getValik_j()].getComponent(Integer.parseInt(model.getSudokuButtons()[model
            .getValik_i()][model.getValik_j()].getNumber()) - 1)).getText().equals(
            model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber())) {
          leidub = true;
          ((JLabel) ruut[model.getValik_i()][model.getValik_j()]
              .getComponent(Integer.parseInt(model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber()) - 1))
              .setText(" ");
        }
        if (!leidub) {
          ruut[model.getValik_i()][model.getValik_j()].setText(" ");
          ((JLabel) ruut[model.getValik_i()][model.getValik_j()]
              .getComponent(Integer.parseInt(model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber()) - 1))
              .setText(model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber());
        }
      }
      model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].setNumber(" ");
    } else {
      String vana = ruut[model.getValik_i()][model.getValik_j()].getText();
      resetAll(model.getValik_i(), model.getValik_j());
      ruut[model.getValik_i()][model.getValik_j()].setText(model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber());
      ruut[model.getValik_i()][model.getValik_j()]
          .setForeground(model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getV2rv());
      ruut[model.getValik_i()][model.getValik_j()].setFont(config.getSudokuFont());
      if (config.isComputerhelp()) {
        String uus = model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber();
        if (help1.getBackground().equals(Color.WHITE)) {
          if (vana.equals("1")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("1") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help2.getBackground().equals(Color.WHITE)) {
          if (vana.equals("2")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("2") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help3.getBackground().equals(Color.WHITE)) {
          if (vana.equals("3")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("3") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help4.getBackground().equals(Color.WHITE)) {
          if (vana.equals("4")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("4") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help5.getBackground().equals(Color.WHITE)) {
          if (vana.equals("5")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("5") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help6.getBackground().equals(Color.WHITE)) {
          if (vana.equals("6")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("6") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help7.getBackground().equals(Color.WHITE)) {
          if (vana.equals("7")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("7") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help8.getBackground().equals(Color.WHITE)) {
          if (vana.equals("8")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("8") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help9.getBackground().equals(Color.WHITE)) {
          if (vana.equals("9")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("9") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help10.getBackground().equals(Color.WHITE)) {
          if (vana.equals("10")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("10") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help11.getBackground().equals(Color.WHITE)) {
          if (vana.equals("11")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("11") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help12.getBackground().equals(Color.WHITE)) {
          if (vana.equals("12")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("12") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help13.getBackground().equals(Color.WHITE)) {
          if (vana.equals("13")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("13") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help14.getBackground().equals(Color.WHITE)) {
          if (vana.equals("14")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("14") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help15.getBackground().equals(Color.WHITE)) {
          if (vana.equals("15")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("15") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
        if (help16.getBackground().equals(Color.WHITE)) {
          if (vana.equals("16")) {
            helpbuttonUpdateVana(vana);
          }
          if (uus.equals("16") || uus.equals(" ")) {
            helpbuttonUpdateUus(uus);
          }
        }
      } else if (config.isComputerchoicehelpremover()) {
        removeDep(model.getValik_i(), model.getValik_j(), model.getSudokuButtons()[model.getValik_i()][model.getValik_j()].getNumber());
      }
    }
    if (config.isComputerhelp()) {
      if (help1.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("1");
      } else if (help2.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("2");
      } else if (help3.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("3");
      } else if (help4.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("4");
      } else if (help5.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("5");
      } else if (help6.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("6");
      } else if (help7.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("7");
      } else if (help8.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("8");
      } else if (help9.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("9");
      } else if (help10.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("10");
      } else if (help11.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("11");
      } else if (help12.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("12");
      } else if (help13.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("13");
      } else if (help14.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("14");
      } else if (help15.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("15");
      } else if (help16.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("16");
      } else if (helpall.getForeground().equals(Config.GREEN)) {
        colorHelpbutton("999");
      }
    }
    ruut[model.getValik_i()][model.getValik_j()].revalidate();
    model.setAbi(false);
  }

  /**
   * Seadistab mängu pausi järgi. Kui paus on peal, siis on näha ainult pausi
   * silt.
   */
  public void paus() {
    if (model.isOnPaus()) {
      m2nguTimer.stop();
      uuesti_button.setEnabled(false);
      ((CardLayout) paus_panel.getLayout()).next(paus_panel);
    } else {
      m2nguTimer.start();
      uuesti_button.setEnabled(true);
      ((CardLayout) paus_panel.getLayout()).next(paus_panel);
    }
  }

  /**
   * Muudab antud nupuga seotud nuppude antud numbriga abitekstid tühjaks
   * 
   * @param i
   * @param j
   * @param button
   *          abiteksti number
   */
  private void removeDep(int i, int j, String button) {
    for (int k = 0; k < ruut.length; k++) {
      if (model.getSudokuButtons()[i][k].getNumber().equals(" ")) {
        ((JLabel) ruut[i][k].getComponent(Integer.parseInt(button) - 1)).setText(" ");
      }
      if (model.getSudokuButtons()[k][j].getNumber().equals(" ")) {
        ((JLabel) ruut[k][j].getComponent(Integer.parseInt(button) - 1)).setText(" ");
      }
    }
    int rida = (i / config.getKast()) * config.getKast();
    int veerg = (j / config.getKast()) * config.getKast();
    for (int k = rida; k < rida + config.getKast(); k++) {
      for (int l = veerg; l < veerg + config.getKast(); l++) {
        if (model.getSudokuButtons()[k][l].getNumber().equals(" ")) {
          ((JLabel) ruut[k][l].getComponent(Integer.parseInt(button) - 1)).setText(" ");
        }
      }
    }
  }

  /**
   * Muudab kõik vastav abinupud tühjaks
   * 
   * @param button
   *          abinupu number
   */
  private void removeHelpbutton(String button) {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (model.getSudokuButtons()[i][j].getNumber().equals(" ")) {
          JLabel l = (JLabel) ruut[i][j].getComponent(Integer.parseInt(button) - 1);
          l.setText(" ");
          l.setOpaque(false);
          l.setBackground(Color.WHITE);
          l.removeMouseListener(this);
        }
      }
    }
  }

  /**
   * Värvib kõik lightgray backgroundiga nupud eelmise värviga
   */
  private void removeLightgray() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (ruut[i][j].getBackground().equals(Color.LIGHT_GRAY)) {
          ruut[i][j].setBackground(eelmine);
        }
      }
    }
  }

  /**
   * Seab antud nupu kõik abinupud " "
   * 
   * @param i
   * @param j
   */
  private void resetAll(int i, int j) {
    for (int k = 0; k < config.getRuudustik(); k++) {
      ((JLabel) ruut[i][j].getComponent(k)).setText(" ");
    }
  }

  /**
   * Kõik värvijad mitteaktiivseks
   */
  public void resetColorers() {
    green.setBackground(Config.GREEN);
    orange.setBackground(Config.ORANGE);
    cyan.setBackground(Config.CYAN);
    pink.setBackground(Config.PINK);
  }

  /**
   * Hetkel midagi
   */
  public void resizeM2ng() {

  }

  /**
   * Seadistab lahendamise menüü nupud. Kui arvutiabi on väljas, ei saa neid
   * nuppe valida. Kui mäng ei ole alustatud, ei saa neid nuppe valida. Kui mäng
   * on pausi peal, ei saa neid nuppe valida.
   */
  public void seadistaLahendajad() {
    if (!model.isOnPaus() && model.isOnAlustatud()) {
      if (config.isComputerhinter()) {
        lahenda_yks_menu.setEnabled(true);
      } else {
        lahenda_yks_menu.setEnabled(false);
      }
      if (config.isComputersolver()) {
        lahenda_menu.setEnabled(true);
      } else {
        lahenda_menu.setEnabled(false);
      }

    } else {
      lahenda_menu.setEnabled(false);
      lahenda_yks_menu.setEnabled(false);
    }
    if (config.getRuudustik() != 16) {
      help10.setVisible(false);
      help11.setVisible(false);
      help12.setVisible(false);
      help13.setVisible(false);
      help14.setVisible(false);
      help15.setVisible(false);
      help16.setVisible(false);
    } else {
      help10.setVisible(true);
      help11.setVisible(true);
      help12.setVisible(true);
      help13.setVisible(true);
      help14.setVisible(true);
      help15.setVisible(true);
      help16.setVisible(true);
    }
    if (config.isComputerhelp() && model.isOnAlustatud() && !model.isOnPaus()) {
      help1.setEnabled(true);
      help2.setEnabled(true);
      help3.setEnabled(true);
      help4.setEnabled(true);
      help5.setEnabled(true);
      help6.setEnabled(true);
      help7.setEnabled(true);
      help8.setEnabled(true);
      help9.setEnabled(true);
      help10.setEnabled(true);
      help11.setEnabled(true);
      help12.setEnabled(true);
      help13.setEnabled(true);
      help14.setEnabled(true);
      help15.setEnabled(true);
      help16.setEnabled(true);
      helpall.setEnabled(true);
      green.setEnabled(true);
      orange.setEnabled(true);
      cyan.setEnabled(true);
      pink.setEnabled(true);
    } else {
      if (model.isOnAlustatud() && !model.isOnPaus()) {
        green.setEnabled(true);
        orange.setEnabled(true);
        cyan.setEnabled(true);
        pink.setEnabled(true);
      } else {
        green.setEnabled(false);
        orange.setEnabled(false);
        cyan.setEnabled(false);
        pink.setEnabled(false);
      }
      help1.setEnabled(false);
      help2.setEnabled(false);
      help3.setEnabled(false);
      help4.setEnabled(false);
      help5.setEnabled(false);
      help6.setEnabled(false);
      help7.setEnabled(false);
      help8.setEnabled(false);
      help9.setEnabled(false);
      help10.setEnabled(false);
      help11.setEnabled(false);
      help12.setEnabled(false);
      help13.setEnabled(false);
      help14.setEnabled(false);
      help15.setEnabled(false);
      help16.setEnabled(false);
      helpall.setEnabled(false);
    }
    if (!model.isOnAlustatud()) {
      help1.setBackground(DEFAULT);
      help2.setBackground(DEFAULT);
      help3.setBackground(DEFAULT);
      help4.setBackground(DEFAULT);
      help5.setBackground(DEFAULT);
      help6.setBackground(DEFAULT);
      help7.setBackground(DEFAULT);
      help8.setBackground(DEFAULT);
      help9.setBackground(DEFAULT);
      help10.setBackground(DEFAULT);
      help11.setBackground(DEFAULT);
      help12.setBackground(DEFAULT);
      help13.setBackground(DEFAULT);
      help14.setBackground(DEFAULT);
      help15.setBackground(DEFAULT);
      help16.setBackground(DEFAULT);
      help1.setForeground(Color.BLACK);
      help2.setForeground(Color.BLACK);
      help3.setForeground(Color.BLACK);
      help4.setForeground(Color.BLACK);
      help5.setForeground(Color.BLACK);
      help6.setForeground(Color.BLACK);
      help7.setForeground(Color.BLACK);
      help8.setForeground(Color.BLACK);
      help9.setForeground(Color.BLACK);
      help10.setForeground(Color.BLACK);
      help11.setForeground(Color.BLACK);
      help12.setForeground(Color.BLACK);
      help13.setForeground(Color.BLACK);
      help14.setForeground(Color.BLACK);
      help15.setForeground(Color.BLACK);
      help16.setForeground(Color.BLACK);
      helpall.setForeground(Color.BLACK);
      helpall.setBackground(DEFAULT);
      green.setBackground(Config.GREEN);
      cyan.setBackground(Config.CYAN);
      orange.setBackground(Config.ORANGE);
      pink.setBackground(Config.PINK);
    }
  }

  /**
   * Teeb undo/redo nuppud nähtavaks või mitte vastavalt stacki suurusele
   */
  public void seadistaUndoRedo() {
    if (model.isOnPaus()) {
      undo.setEnabled(false);
      undo_menu.setEnabled(false);
      undo.setEnabled(false);
      redo_menu.setEnabled(false);
    } else {
      if (model.isOnAlustatud() && model.getUndo().size() > 0 && !(model.getUndo().getLast()).isAlustatud()) {
        undo.setEnabled(false);
        undo_menu.setEnabled(false);
      } else if (model.getUndo().size() > 0) {
        undo.setEnabled(true);
        undo_menu.setEnabled(true);
      } else {
        undo.setEnabled(false);
        undo_menu.setEnabled(false);
      }
      int nr = 0;
      if (model.getUndo().size() > 0) {
        nr = model.getUndo().size() - 1;
      }
      if (model.isOnAlustatud() && model.getRedo().size() > 0 && !(model.getRedo().get(nr)).isAlustatud()) {
        redo.setEnabled(false);
        redo_menu.setEnabled(false);
      }
      if (model.getRedo().size() > model.getUndo().size()) {
        redo.setEnabled(true);
        redo_menu.setEnabled(true);
      } else {
        redo.setEnabled(false);
        redo_menu.setEnabled(false);
      }
    }
  }

  /**
   * Setter
   * 
   * @param data
   */
  public void setData(Data data) {
    this.data = data;
  }

  /**
   * Setter
   * 
   * @param helpUI
   */
  public void setHelpUI(HelpUI helpUI) {
    this.helpUI = helpUI;
  }

  /**
   * Kirjutab raskusastme vastavalt model.level väärtusele
   */
  public void setLevel() {
    String s = model.getLevel();
    if (s.equals(language.getSudoku_diffeasy())) {
      level.setForeground(new Color(0, 153, 0));
    } else if (s.equals(language.getSudoku_diffmedium())) {
      level.setForeground(Color.ORANGE);
    } else if (s.equals(language.getSudoku_diffhard())) {
      level.setForeground(new Color(255, 0, 255));
    } else if (s.equals(language.getSudoku_diffexpert())) {
      level.setForeground(Color.RED);
    } else if (s.equals(language.getSudoku_diffextreme())) {
      level.setForeground(Color.BLACK);
    }
    level.setText(s);
  }

  /**
   * Kirjutab timeri näidu.
   * 
   * @param aeg
   *          timeri näit
   */
  public void setTime_label(String aeg) {
    time_label.setText(aeg);
    String[] s = aeg.split(":");
    hour = Integer.parseInt(s[0]);
    min = Integer.parseInt(s[1]);
    sec = Integer.parseInt(s[2]);
  }

  /**
   * Värvib antud nupu hinti asjad kollaseks ja enda roheliseks
   * 
   * @param i
   * @param j
   */
  public void solver(int i, int j) {
    si = i;
    sj = j;
    List<List<SudokuButton>> list = model.getSudokuButtons()[i][j].getSeotud();
    for (int m = 0; m < list.size(); m++) {
      List<SudokuButton> list2 = list.get(m);
      for (int n = 0; n < list2.size(); n++) {
        SudokuButton b = list2.get(n);
        ruut[model.getSudokuButtons()[b.getI()][b.getJ()].getI()][model.getSudokuButtons()[b.getI()][b.getJ()].getJ()]
            .setBackground(Color.YELLOW);
      }
    }
    String tehnika = "";
    List<String> tehnikad = model.getSudokuButtons()[i][j].getTehnikad();
    for (int m = 0; m < tehnikad.size(); m++) {
      String s = tehnikad.get(m);
      tehnika = ", " + s + tehnika;
    }
    status(tehnika.substring(1), Color.YELLOW);
    ruut[i][j].setBackground(Color.GREEN);
  }

  /**
   * Status tekst värvi v2rv
   * 
   * @param tekst
   * @param v2rv
   */
  public void status(String tekst, Color v2rv) {
    status.setText(tekst);
    status.setBackground(v2rv);
  }

  /**
   * Olemasolev tekst värvi v2rv
   * 
   * @param v2rv
   */
  public void status(Color v2rv) {
    status(status.getText(), v2rv);
  }

  /**
   * Status tekst default värvi
   * 
   * @param tekst
   */
  public void status(String tekst) {
    status(tekst, DEFAULT);
  }

  /**
   * Olemasolevale tekstile lisatakse tekst default värvi
   * 
   * @param tekst
   */
  public void statusAdd(String tekst) {
    status(status.getText() + tekst, DEFAULT);
  }

  /**
   * Uuendab kõiki menüü stringe, et keele muutmine oleks dünaamiline
   */
  public void sudokuUI() {
    kasutaja.setText(language.getConfig_nimi());
    m2ng.setText(language.getSudoku_m2ng());
    m2ngud.setText(language.getSudoku_m2ngud());
    keel.setText(language.getSudoku_keel());
    seaded.setText(language.getSudoku_seaded());
    ajal.setText(language.getSudoku_ajal());
    abim.setText(language.getSudoku_abi());
    eesti.setText(language.getSudoku_eesti());
    inglise.setText(language.getSudoku_inglise());
    ajalugu.setText(language.getSudoku_ajalugu());
    ava_menu.setText(language.getSudoku_ava_menu());
    salvesta_menu.setText(language.getSudoku_salvesta_menu());
    uus_menu.setText(language.getSudoku_uus_menu());
    print_menu.setText(language.getSudoku_print_menu());
    v2lju_menu.setText(language.getSudoku_v2lju_menu());
    abi_menu.setText(language.getSudoku_abi_menu());
    about.setText(language.getSudoku_about());
    updater.setText(language.getSudoku_updater());
    lahenda_menu.setText(language.getSudoku_lahenda_menu());
    vali_menu9.setText(language.getSudoku_vali_menu9());
    vali_menu16.setText(language.getSudoku_vali_menu16());
    genereeri.setText(language.getSudoku_genereeri());
    lahenda_yks_menu.setText(language.getSudoku_lahenda_yks_menu());
    kasutaja_menu.setText(language.getSudoku_kasutaja_menu());
    computerhelp.setText(language.getSudoku_computerhelp());
    ruudustik9.setText(language.getSudoku_ruudustik9());
    ruudustik16.setText(language.getSudoku_ruudustik16());
    alusta_button.setText(language.getSudoku_alusta_button());
    paus_button.setText(language.getSudoku_paus_button());
    uuesti_button.setText(language.getSudoku_uuesti_button());
    paus_label.setText(language.getSudoku_paus_label());
    diffeasy.setText(language.getSudoku_diffeasy());
    diffmedium.setText(language.getSudoku_diffmedium());
    diffhard.setText(language.getSudoku_diffhard());
    diffexpert.setText(language.getSudoku_diffexpert());
    diffextreme.setText(language.getSudoku_diffextreme());
    symm.setText(language.getSudoku_symm());
    lisa_menu.setText(language.getSudoku_lisa_menu());
    view.setText(language.getSudoku_view());
    undo_menu.setText(language.getSudoku_undo());
    redo_menu.setText(language.getSudoku_redo());
    alusta_menu.setText(language.getSudoku_alusta_button());
    paus_menu.setText(language.getSudoku_paus_button());
    fonts_menu.setText(language.getSudoku_fonts_menu());
    background.setToolTipText(language.getsudoku_background());
    if (model.getNimi().equals("Nimetu") || model.getNimi().equals("Unnamed") || model.getNimi().equals("Namenlos")) {
      if (config.getLanguage().equals("est")) {
        model.setNimi("Nimetu");
      } else if (config.getLanguage().equals("eng")) {
        model.setNimi("Unnamed");
      }
      uuendaTitle();
    }
    setLevel();
    validate();
  }

  /**
   * Suurendab timeri näitu. Iga timeri sündmuse korral, suurendab näitu sekundi
   * võrra.
   */
  public void timer() {
    sec++;
    if (sec == 60) {
      sec = 0;
      min++;
    }
    if (min == 60) {
      min = 0;
      hour++;
    }
    String aeg = "";
    if (hour < 10) {
      aeg += "0";
    }
    aeg += String.valueOf(hour) + ":";
    if (min < 10) {
      aeg += "0";
    }
    aeg += String.valueOf(min) + ":";
    if (sec < 10) {
      aeg += "0";
    }
    aeg += String.valueOf(sec);
    time_label.setText(aeg);
  }

  /**
   * Kontrollib kas on vaja uuendada. Kui on vaja, siis kutsub R-Sudoku.jari
   * update parameetriga
   * 
   * @param ise
   *          kas näidata teadet uuendamise vajalikkusest
   */
  public void update(boolean ise) {
    try {
      BufferedReader re = new BufferedReader(new InputStreamReader(
          (new URL("http://www.hot.ee/rometpiho/sudoku/versioon.ver").openConnection()).getInputStream()));
      if (!re.readLine().equals(config.getVersioon())) {
        JOptionPane pane = new JOptionPane(language.getUpdater_message1());
        pane.setOptions(new Object[] { language.getButton_yes(), language.getButton_no() });
        JDialog dialog = pane.createDialog(this, language.getController_title());
        dialog.setVisible(true);
        Object selectedValue = pane.getValue();
        if (selectedValue.equals(language.getButton_yes())) {
          re.close();
          if (new File("R-Sudoku.jar").exists()) {
            Runtime.getRuntime().exec("java -jar R-Sudoku.jar update");
          } else {
            Runtime.getRuntime().exec("java -jar Sudoku.jar update");
          }
          dispose();
        }
      } else if (ise) {
        JOptionPane.showMessageDialog(this, language.getUpdater_message2(), language.getMessage(), JOptionPane.INFORMATION_MESSAGE);
      }
      re.close();
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(this, language.getUpdater_error(), language.getMessage(), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception e) {
      Config.log(e.getClass().toString() + ": " + e.getMessage(), e.getStackTrace());
    }
  }

  /**
   * Uuendab kõik ruudud. Uuendamiseks saadakse andmed mängu loogikast.
   */
  public void uuendaUI() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        for (int k = 0; k < ruut[i][j].getComponentCount(); k++) {
          if (!model.isAbi()) {
            ruut[i][j].getComponent(k).setForeground(config.getHelpButton());
            ruut[i][j].getComponent(k).setFont(config.getHelpFont());
            ((JLabel) ruut[i][j].getComponent(k)).setOpaque(false);
          }
        }
        ruut[i][j].setFont(config.getSudokuFont());
        ruut[i][j].setText(model.getSudokuButtons()[i][j].getNumber());
        ruut[i][j].setForeground(model.getSudokuButtons()[i][j].getV2rv());
        ruut[i][j].validate();
        ruut[i][j].revalidate();
        ruut[i][j].repaint();
        ruut[i][j].doLayout();
      }
    }
    kasutaja.setText(config.getNimi());
    seadistaUndoRedo();
  }

  /**
   * Teeb kõik peale värvijate ja lightgreen backgroundiga nuppude backgroundid
   * defaultiks
   */
  public void uuendaBackground() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (!ruut[i][j].getBackground().equals(Config.GREEN) && !ruut[i][j].getBackground().equals(Config.CYAN)
            && !ruut[i][j].getBackground().equals(Config.ORANGE) && !ruut[i][j].getBackground().equals(Config.PINK)
            && !ruut[i][j].getBackground().equals(Config.LIGHTGREEN)) {
          ruut[i][j].setBackground((config.getSudokuButton()));
        }
      }
    }
  }

  /**
   * Teeb kõik nuppude backgroundid defaultiks
   */
  public void uuendaBackgroundAll() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        ruut[i][j].setBackground((config.getSudokuButton()));
        for (int k = 0; k < ruut[i][j].getComponentCount(); k++) {
          ruut[i][j].getComponent(k).setForeground(config.getHelpButton());
          ((JLabel) ruut[i][j].getComponent(k)).setOpaque(false);
        }
        ruut[i][j].validate();
      }
    }
  }

  /**
   * Teeb kõik lightgreen backgroundiga nupud default taustaga
   */
  public void uuendaBackgroundLightgreen() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (ruut[i][j].getBackground().equals(Config.LIGHTGREEN)) {
          ruut[i][j].setBackground((config.getSudokuButton()));
        }
      }
    }
  }

  /**
   * Muudab vastava ruudu backgroundi eelmiseks tagasi
   * 
   * @param i
   * @param j
   */
  public void uuendaBackground(int i, int j) {
    ruut[i][j].setBackground(eelmine);
  }

  /**
   * Uuendab akna nime
   */
  public void uuendaTitle() {
    setTitle("R-Sudoku #" + model.getNimi());
  }

  /**
   * Uuesti nupu vajutamise realiseerimine. Kirjutab mängu algseisu ja seadistab
   * vastavalt alumise menüü nupud.
   */
  public void uuesti() {
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        if (ruut[i][j].getComponents().length > 0) {
          resetAll(i, j);
          ruut[i][j].setForeground(model.getSudokuButtons()[i][j].getV2rv());
          ruut[i][j].setFont(config.getSudokuFont());
        }
        ruut[i][j].setText(model.getSudokuButtons()[i][j].getNumber());
        ruut[i][j].setForeground(model.getSudokuButtons()[i][j].getV2rv());
        ruut[i][j].validate();
        ruut[i][j].revalidate();
        ruut[i][j].repaint();
        ruut[i][j].doLayout();
      }
    }
    paus_button.setEnabled(false);
    alusta_button.setEnabled(true);
    uuesti_button.setEnabled(false);
    alusta_menu.setEnabled(true);
    paus_menu.setEnabled(false);
    m2nguTimer.stop();
  }

  /**
   * Teeb nuppude ja alumise menüü nuppude paneeli.
   * 
   * @return nuppude ja alumise menüü nuppude paneel
   */
  public JPanel uusPaneel() {
    sec = 0;
    min = 0;
    hour = 0;
    m2nguTimer = new Timer(1000, this);

    alusta_menu.setEnabled(true);
    paus_menu.setEnabled(false);
    model.setLevel("");
    level.setText(model.getLevel());
    ruut = new JButton[config.getRuudustik()][config.getRuudustik()];
    for (int i = 0; i < config.getRuudustik(); i++) {
      for (int j = 0; j < config.getRuudustik(); j++) {
        ruut[i][j] = new JButton(" ");
        ruut[i][j].setBackground(config.getSudokuButton());
        ruut[i][j].setFocusPainted(false);
        ruut[i][j].setFont(config.getSudokuFont());
        ruut[i][j].addMouseListener(this);
        ruut[i][j].addKeyListener(this);
        ruut[i][j].setLayout(new GridLayout(config.getKast(), config.getKast()));
        for (int k = 0; k < config.getRuudustik(); k++) {
          final JLabel label;
          if (k > 9) {
            label = new JLabel("    ");
          } else {
            label = new JLabel("   ");
          }
          label.setFont(config.getHelpFont());
          label.setForeground(config.getHelpButton());
          label.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
              if (e.getButton() == 3 && !label.getText().trim().isEmpty()) {
                boolean eq = label.isOpaque() && (label.getBackground().equals(Color.BLACK) || label.getBackground().equals(Color.RED));
                Color f;
                if (e.isControlDown()) {
                  eq &= label.getForeground().equals(Color.CYAN);
                  f = eq ? config.getHelpButton() : Color.CYAN;
                } else {
                  eq &= label.getForeground().equals(Color.WHITE);
                  f = eq ? config.getHelpButton() : Color.WHITE;
                }
                Color b = eq ? Color.WHITE : label.isOpaque() ? Color.RED : Color.BLACK;
                label.setBackground(b);
                label.setForeground(f);
                label.setOpaque(!eq);
              } else {
                e.setSource(label.getParent());
                label.getParent().dispatchEvent(e);
              }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseClicked(MouseEvent e) {
            }
          });
          ruut[i][j].add(label);
        }
      }
    }
    /*
     * 1 2 3 10 4 5 6 11 7 8 9 12 13 14 15 16
     */

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p1.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = 0; i < config.getKast(); i++) {
      for (int j = 0; j < config.getKast(); j++) {
        p1.add(ruut[i][j]);
      }
    }
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p2.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = 0; i < config.getKast(); i++) {
      for (int j = config.getKast(); j < (config.getKast() * 2); j++) {
        p2.add(ruut[i][j]);
      }
    }
    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p3.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = 0; i < config.getKast(); i++) {
      for (int j = (config.getKast() * 2); j < (config.getKast() * 3); j++) {
        p3.add(ruut[i][j]);
      }
    }
    JPanel p4 = new JPanel();
    p4.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p4.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = config.getKast(); i < (config.getKast() * 2); i++) {
      for (int j = 0; j < config.getKast(); j++) {
        p4.add(ruut[i][j]);
      }
    }
    JPanel p5 = new JPanel();
    p5.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p5.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = config.getKast(); i < (config.getKast() * 2); i++) {
      for (int j = config.getKast(); j < (config.getKast() * 2); j++) {
        p5.add(ruut[i][j]);
      }
    }
    JPanel p6 = new JPanel();
    p6.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p6.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = config.getKast(); i < (config.getKast() * 2); i++) {
      for (int j = (config.getKast() * 2); j < (config.getKast() * 3); j++) {
        p6.add(ruut[i][j]);
      }
    }
    JPanel p7 = new JPanel();
    p7.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p7.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = (config.getKast() * 2); i < (config.getKast() * 3); i++) {
      for (int j = 0; j < config.getKast(); j++) {
        p7.add(ruut[i][j]);
      }
    }
    JPanel p8 = new JPanel();
    p8.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p8.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = (config.getKast() * 2); i < (config.getKast() * 3); i++) {
      for (int j = config.getKast(); j < (config.getKast() * 2); j++) {
        p8.add(ruut[i][j]);
      }
    }
    JPanel p9 = new JPanel();
    p9.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p9.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
    for (int i = (config.getKast() * 2); i < (config.getKast() * 3); i++) {
      for (int j = (config.getKast() * 2); j < (config.getKast() * 3); j++) {
        p9.add(ruut[i][j]);
      }
    }

    JPanel p10 = new JPanel();
    JPanel p11 = new JPanel();
    JPanel p12 = new JPanel();
    JPanel p13 = new JPanel();
    JPanel p14 = new JPanel();
    JPanel p15 = new JPanel();
    JPanel p16 = new JPanel();
    if (config.getRuudustik() == 16) {
      p10.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p10.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = 0; i < config.getKast(); i++) {
        for (int j = (config.getKast() * 3); j < (config.getKast() * 4); j++) {
          p10.add(ruut[i][j]);
        }
      }
      p11.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p11.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast()); i < (config.getKast() * 2); i++) {
        for (int j = (config.getKast() * 3); j < (config.getKast() * 4); j++) {
          p11.add(ruut[i][j]);
        }
      }
      p12.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p12.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast() * 2); i < (config.getKast() * 3); i++) {
        for (int j = (config.getKast() * 3); j < (config.getKast() * 4); j++) {
          p12.add(ruut[i][j]);
        }
      }
      p13.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p13.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast() * 3); i < (config.getKast() * 4); i++) {
        for (int j = 0; j < (config.getKast()); j++) {
          p13.add(ruut[i][j]);
        }
      }
      p14.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p14.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast() * 3); i < (config.getKast() * 4); i++) {
        for (int j = (config.getKast()); j < (config.getKast() * 2); j++) {
          p14.add(ruut[i][j]);
        }
      }
      p15.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p15.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast() * 3); i < (config.getKast() * 4); i++) {
        for (int j = (config.getKast() * 2); j < (config.getKast() * 3); j++) {
          p15.add(ruut[i][j]);
        }
      }
      p16.setLayout(new GridLayout(config.getKast(), config.getKast()));
      p16.setBorder(BorderFactory.createLineBorder(kasti_raamid, kasti_paksus));
      for (int i = (config.getKast() * 3); i < (config.getKast() * 4); i++) {
        for (int j = (config.getKast() * 3); j < (config.getKast() * 4); j++) {
          p16.add(ruut[i][j]);
        }
      }
    }

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(config.getKast(), config.getKast()));
    p.add(p1);
    p.add(p2);
    p.add(p3);
    if (config.getRuudustik() == 16) {
      p.add(p10);
    }
    p.add(p4);
    p.add(p5);
    p.add(p6);
    if (config.getRuudustik() == 16) {
      p.add(p11);
    }
    p.add(p7);
    p.add(p8);
    p.add(p9);
    if (config.getRuudustik() == 16) {
      p.add(p12);
      p.add(p13);
      p.add(p14);
      p.add(p15);
      p.add(p16);
    }
    paus_button = new JButton(language.getSudoku_paus_button());
    paus_button.addActionListener(this);

    alusta_button = new JButton(language.getSudoku_alusta_button());
    alusta_button.addActionListener(this);
    if (model.isOnAlustatud()) {
      alusta_button.setEnabled(false);
      paus_button.setEnabled(true);
    } else {
      alusta_button.setEnabled(true);
      paus_button.setEnabled(false);
    }

    time_label = new JLabel("00:00:00");
    time_label.setFont(new Font(null, Font.PLAIN, 30));

    paus_label = new JLabel(language.getSudoku_paus_label());
    paus_label.setFont(new Font(null, Font.BOLD, 30));
    paus_label.setHorizontalAlignment(JLabel.CENTER);

    uuesti_button = new JButton(language.getSudoku_uuesti_button());
    uuesti_button.addActionListener(this);
    uuesti_button.setEnabled(false);

    paus_panel = new JPanel();
    paus_panel.setLayout(new CardLayout());
    paus_panel.add(p, "sudoku");
    paus_panel.add(paus_label, "paus");

    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1, 4));
    pan.add(uuesti_button);
    pan.add(alusta_button);
    pan.add(paus_button);
    pan.add(time_label);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(paus_panel, BorderLayout.CENTER);
    panel.add(pan, BorderLayout.SOUTH);
    uuendaTitle();
    return panel;
  }

  /**
   * Seadistab mängu default seisu.
   */
  public void uusM2ng() {
    if (config.getLanguage().equals("est")) {
      uusM2ng("Nimetu");
    } else if (config.getLanguage().equals("eng")) {
      uusM2ng("Unnamed");
    }
  }

  /**
   * Seadistab teatud nimega mängu default seisu.
   * 
   * @param nimi
   *          mängu nimi
   */
  public void uusM2ng(String nimi) {
    m2nguTimer.stop();
    remove(panel);
    model.setOnAlustatud(false);
    model.setOnPaus(false);
    model.setValitud_m2ng(-1);
    model.setNimi(nimi);
    model.setLevel("");
    level.setText(model.getLevel());
    panel = uusPaneel();
    add(panel, BorderLayout.CENTER);
    pack();
    resizeM2ng();
    validate();
  }

  /**
   * Sissesalvestatud mängude hulgast valitud mängu näitamine.
   */
  public void valitudM2ng() {
    int a = model.getValitud_m2ng();
    uusM2ng(model.getNimi());
    model.setValitud_m2ng(a);
    uuendaTitle();
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        ruut[i][j].setText(model.getSudokuButtons()[i][j].getNumber());
        ruut[i][j].setForeground(model.getSudokuButtons()[i][j].getV2rv());
        ruut[i][j].setBackground(config.getSudokuButton());
      }
    }
  }

  /**
   * Võidu puhul mängu deaktiviseerimine.
   */
  public void v6it() {
    m2nguTimer.stop();
    paus_button.setEnabled(false);
    uuesti_button.setEnabled(false);
    alusta_button.setEnabled(false);
    paus_menu.setEnabled(false);
    lahenda_menu.setEnabled(false);
    lahenda_yks_menu.setEnabled(false);
    for (int i = 0; i < ruut.length; i++) {
      for (int j = 0; j < ruut[i].length; j++) {
        ruut[i][j].setEnabled(false);
        ruut[i][j].removeMouseListener(this);
      }
    }
  }

  @Override
  public void mouseClicked(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
    if (e.getSource().equals(status) && status.getBackground().equals(Color.YELLOW)) {
      List<List<SudokuButton>> list = model.getSudokuButtons()[si][sj].getSeotud();
      for (int i = 0; i < list.size(); i++) {
        List<SudokuButton> list2 = list.get(i);
        for (int j = 0; j < list2.size(); j++) {
          SudokuButton bu = list2.get(j);
          ruut[model.getSudokuButtons()[bu.getI()][bu.getJ()].getI()][model.getSudokuButtons()[bu.getI()][bu.getJ()].getJ()]
              .setBackground(Color.YELLOW);
        }
      }
      ruut[si][sj].setBackground(Color.GREEN);
      status.select(0, 0);
    }
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    if (e.getSource().equals(help1)) {
      if (e.getButton() == 3) {
        if (help1.getForeground().equals(Color.BLACK) && help1.getBackground().equals(DEFAULT)) {
          helpbuttons(1);
        }
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(1);
      }
    } else if (e.getSource().equals(help2)) {
      if (e.getButton() == 3) {
        if (help2.getForeground().equals(Color.BLACK) && help2.getBackground().equals(DEFAULT)) {
          helpbuttons(2);
        }
        help1.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(2);
      }
    } else if (e.getSource().equals(help3)) {
      if (e.getButton() == 3) {
        if (help3.getForeground().equals(Color.BLACK) && help3.getBackground().equals(DEFAULT)) {
          helpbuttons(3);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(3);
      }
    } else if (e.getSource().equals(help4)) {
      if (e.getButton() == 3) {
        if (help4.getForeground().equals(Color.BLACK) && help4.getBackground().equals(DEFAULT)) {
          helpbuttons(4);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(4);
      }
    } else if (e.getSource().equals(help5)) {
      if (e.getButton() == 3) {
        if (help5.getForeground().equals(Color.BLACK) && help5.getBackground().equals(DEFAULT)) {
          helpbuttons(5);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(5);
      }
    } else if (e.getSource().equals(help6)) {
      if (e.getButton() == 3) {
        if (help6.getForeground().equals(Color.BLACK) && help6.getBackground().equals(DEFAULT)) {
          helpbuttons(6);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(6);
      }
    } else if (e.getSource().equals(help7)) {
      if (e.getButton() == 3) {
        if (help7.getForeground().equals(Color.BLACK) && help7.getBackground().equals(DEFAULT)) {
          helpbuttons(7);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(7);
      }
    } else if (e.getSource().equals(help8)) {
      if (e.getButton() == 3) {
        if (help8.getForeground().equals(Color.BLACK) && help8.getBackground().equals(DEFAULT)) {
          helpbuttons(8);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(8);
      }
    } else if (e.getSource().equals(help9)) {
      if (e.getButton() == 3) {
        if (help9.getForeground().equals(Color.BLACK) && help9.getBackground().equals(DEFAULT)) {
          helpbuttons(9);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(9);
      }
    } else if (e.getSource().equals(help10)) {
      if (e.getButton() == 3) {
        if (help10.getForeground().equals(Color.BLACK) && help10.getBackground().equals(DEFAULT)) {
          helpbuttons(10);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(10);
      }
    } else if (e.getSource().equals(help11)) {
      if (e.getButton() == 3) {
        if (help11.getForeground().equals(Color.BLACK) && help11.getBackground().equals(DEFAULT)) {
          helpbuttons(11);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(11);
      }
    } else if (e.getSource().equals(help12)) {
      if (e.getButton() == 3) {
        if (help12.getForeground().equals(Color.BLACK) && help12.getBackground().equals(DEFAULT)) {
          helpbuttons(12);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(12);
      }
    } else if (e.getSource().equals(help13)) {
      if (e.getButton() == 3) {
        if (help13.getForeground().equals(Color.BLACK) && help13.getBackground().equals(DEFAULT)) {
          helpbuttons(13);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(13);
      }
    } else if (e.getSource().equals(help14)) {
      if (e.getButton() == 3) {
        if (help14.getForeground().equals(Color.BLACK) && help14.getBackground().equals(DEFAULT)) {
          helpbuttons(14);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(14);
      }
    } else if (e.getSource().equals(help15)) {
      if (e.getButton() == 3) {
        if (help15.getForeground().equals(Color.BLACK) && help15.getBackground().equals(DEFAULT)) {
          helpbuttons(15);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(15);
      }
    } else if (e.getSource().equals(help16)) {
      if (e.getButton() == 3) {
        if (help16.getForeground().equals(Color.BLACK) && help16.getBackground().equals(DEFAULT)) {
          helpbuttons(16);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        helpall.setForeground(Color.BLACK);
        helpButtonsColors(16);
      }
    } else if (e.getSource().equals(helpall)) {
      if (e.getButton() == 3) {
        if (helpall.getForeground().equals(Color.BLACK) && helpall.getBackground().equals(DEFAULT)) {
          helpbuttons(999);
        }
        help1.setForeground(Color.BLACK);
        help2.setForeground(Color.BLACK);
        help3.setForeground(Color.BLACK);
        help4.setForeground(Color.BLACK);
        help5.setForeground(Color.BLACK);
        help6.setForeground(Color.BLACK);
        help7.setForeground(Color.BLACK);
        help8.setForeground(Color.BLACK);
        help9.setForeground(Color.BLACK);
        help10.setForeground(Color.BLACK);
        help11.setForeground(Color.BLACK);
        help12.setForeground(Color.BLACK);
        help13.setForeground(Color.BLACK);
        help14.setForeground(Color.BLACK);
        help15.setForeground(Color.BLACK);
        help16.setForeground(Color.BLACK);
        helpButtonsColors(999);
      }
    } else {
      for (int i = 0; i < config.getRuudustik(); i++) {
        for (int j = 0; j < config.getRuudustik(); j++) {
          if (e.getSource().equals(ruut[i][j])) {
            model.setValik_i(i);
            model.setValik_j(j);
            Color temp = ruut[i][j].getBackground();
            removeLightgray();
            eelmine = ruut[i][j].getBackground();
            if (!model.isOnAlustatud() || !model.getSudokuButtons()[i][j].getV2rv().equals(config.getEsialgne())) {
              ruut[i][j].setBackground(Color.LIGHT_GRAY);
            }
            if (temp.equals(Color.LIGHT_GRAY)) {
              ruut[i][j].setBackground(eelmine);
            }
            if (e.getButton() == 3) {
              controller.handleEvent(Controller.VALIK_AKEN_CLOSE_EVENT);
              ruut[i][j].requestFocus();
            } else if (e.getButton() == 1) {
              if (green.getBackground().equals(Color.WHITE)) {
                if (temp.equals(Config.GREEN)) {
                  ruut[i][j].setBackground(Color.WHITE);
                } else {
                  ruut[i][j].setBackground(Config.GREEN);
                }
              } else if (cyan.getBackground().equals(Color.WHITE)) {
                if (temp.equals(Config.CYAN)) {
                  ruut[i][j].setBackground(Color.WHITE);
                } else {
                  ruut[i][j].setBackground(Config.CYAN);
                }
              } else if (orange.getBackground().equals(Color.WHITE)) {
                if (temp.equals(Config.ORANGE)) {
                  ruut[i][j].setBackground(Color.WHITE);
                } else {
                  ruut[i][j].setBackground(Config.ORANGE);
                }
              } else if (pink.getBackground().equals(Color.WHITE)) {
                if (temp.equals(Config.PINK)) {
                  ruut[i][j].setBackground(Color.WHITE);
                } else {
                  ruut[i][j].setBackground(Config.PINK);
                }
              } else {
                if (MouseEvent.getMouseModifiersText(e.getModifiers()).contains("Ctrl")) {
                  abi = true;
                }
                model.setAbi(abi);
                abi = false;
                controller.handleEvent(Controller.VALIK_AKEN_EVENT);
              }
            } else if (model.isOnAlustatud() && e.getButton() == 2) {
              abi = true;
              model.setAbi(abi);
              abi = false;
              controller.handleEvent(Controller.VALIK_AKEN_EVENT);
            }
          }
        }
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseDragged(MouseEvent e) {

  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseMoved(MouseEvent e) {
    if (e.getSource().equals(status) && status.getBackground().equals(Color.YELLOW)) {
      String[] s = status.getText().split(",");
      int r = 0;
      int t = 0;
      boolean b = true;
      for (int i = 0; i < s.length; i++) {
        JLabel lab = new JLabel(s[i]);
        r += lab.getPreferredSize().width;
        t += s[i].length();
        if (e.getX() < r) {
          status.select(t - s[i].length(), t);
          List<List<SudokuButton>> list = model.getSudokuButtons()[si][sj].getSeotud();
          List<SudokuButton> real = new ArrayList<SudokuButton>();
          for (int m = 0; m < list.size(); m++) {
            List<SudokuButton> list2 = list.get(m);
            for (int n = 0; n < list2.size(); n++) {
              SudokuButton bu = list2.get(n);
              if (list.indexOf(list2) == s.length - 1 - i) {
                real = list2;
              } else {
                ruut[model.getSudokuButtons()[bu.getI()][bu.getJ()].getI()][model.getSudokuButtons()[bu.getI()][bu.getJ()].getJ()]
                    .setBackground(Color.YELLOW);
              }
            }
          }
          for (int j = 0; j < real.size(); j++) {
            SudokuButton bu = real.get(j);
            ruut[model.getSudokuButtons()[bu.getI()][bu.getJ()].getI()][model.getSudokuButtons()[bu.getI()][bu.getJ()].getJ()]
                .setBackground(Color.CYAN);
          }
          if (!ruut[si][sj].getBackground().equals(Color.CYAN)) {
            ruut[si][sj].setBackground(Color.GREEN);
          }
          b = false;
          status.requestFocus();
          return;
        }
        JLabel lab1 = new JLabel(",");
        r += lab1.getPreferredSize().width;
        t += 1;
      }
      if (b) {
        List<List<SudokuButton>> list = model.getSudokuButtons()[si][sj].getSeotud();
        for (int i = 0; i < list.size(); i++) {
          List<SudokuButton> list2 = list.get(i);
          for (int j = 0; j < list2.size(); j++) {
            SudokuButton bu = list2.get(j);
            ruut[model.getSudokuButtons()[bu.getI()][bu.getJ()].getI()][model.getSudokuButtons()[bu.getI()][bu.getJ()].getJ()]
                .setBackground(Color.YELLOW);
          }
        }
        ruut[si][sj].setBackground(Color.GREEN);
        status.select(0, 0);
      }
      status.requestFocus();
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
   */
  @Override
  public void windowActivated(WindowEvent e) {

  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
   */
  @Override
  public void windowClosed(WindowEvent e) {
    close();
  }

  private void close() {
    Point location = getLocation();
    config.setLocation_x(location.x);
    config.setLocation_y(location.y);
    config.salvesta();
    if (model.thread != null && model.thread.isAlive()) {
      while (model.thread.isAlive()) {
        model.generate.done = true;
        try {
          Thread.sleep(100);
        } catch (Exception er) {
          Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
        }
      }
    }
    if (helpUI.isShowing()) {
      helpUI.dispose();
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
   */
  @Override
  public void windowClosing(WindowEvent e) {
    close();
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
   */
  @Override
  public void windowDeactivated(WindowEvent e) {

  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
   */
  @Override
  public void windowDeiconified(WindowEvent e) {

  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
   */
  @Override
  public void windowIconified(WindowEvent e) {

  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
   */
  @Override
  public void windowOpened(WindowEvent e) {

  }
}
